10 & 6 1010 & 0110 0010 1010 | 0110 1110 1010 ^ 0110 1100 101000 &100000 100000 101110 & 1110 1110 horizonal 14,112,896 vertical 146,292,584 diagonal 168,546 #include #include using namespace std; //bit-wize operations // and or xor // & | ^ void dispalyGameBoard(int xMoves, int oMoves); bool positionTaken(int position, int xMoves, int oMoves) { bool result = false; if ((xMoves & (int)pow(2.0,position)) == (int)pow(2.0,position) || (oMoves & (int)pow(2.0,position)) == (int)pow(2.0,position)) { result = true; } return result; } bool positionTaken(int position, int moves) { bool result = false; if ((moves & (int)pow(2.0,position)) == (int)pow(2.0,position)) { result = true; } return result; } bool gameOver(int xMoves, int oMoves) { bool result = false; if((xMoves & 14) == 14 || (xMoves & 112) == 112 || (xMoves & 896) == 896 || (xMoves & 146) == 146 || (xMoves & 292) == 292 || (xMoves & 584) == 584 || (xMoves & 168) == 168 || (xMoves & 546) == 546 ) { result = true; } return result; } void main() { int xMoves = 0; int oMoves = 0; char turn = 'X'; int move; do { dispalyGameBoard(xMoves,oMoves); //needs to be between 1 and 9 and can not be a spot already taken do { cout << turn << "'s turn: "; cin >> move; } while(move < 1 || move > 9 || positionTaken(move,xMoves,oMoves)); if(turn == 'X') { xMoves += (int)pow(2.0,move); turn = 'O'; } else { oMoves += (int)pow(2.0,move); turn = 'X'; } } while(!gameOver(xMoves,oMoves)); //stop when there is a winner or the board is full } void dispalyGameBoard(int xMoves, int oMoves) { for(int i = 1; i <= 9; i++) { if(positionTaken(i,xMoves)) { cout << "X"; } else if(positionTaken(i,oMoves)) { cout << "O"; } else { cout << i; } if(i ==3 || i == 6) { cout << endl << "-----" << endl; } else if (i == 9) { cout << endl; } else { cout << "|"; } } }